Thread: Visual Studio C++, syntax error : ']'

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Quote Originally Posted by Sparrowhawk View Post
    Making another array is easy enough with a loop, that would then get destroyed I assume because its scope is only inside the reverse function.
    Yes, you can replace memcpy with a loop.
    And no, it won't die because it would be allocated with malloc and malloc requires free.

    I was just looking for a more efficient way I suppose. I hate throwing more lines of code at stuff if there's a neater/easier/cleaner way to do it.
    C is not exactly good at this generic stuff.
    The closes thing is a macro, but they are ugly and not very type-safe and hard to debug.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by Elysia View Post
    Yes, you can replace memcpy with a loop.
    And no, it won't die because it would be allocated with malloc and malloc requires free.


    C is not exactly good at this generic stuff.
    The closes thing is a macro, but they are ugly and not very type-safe and hard to debug.
    I'm having some trouble conceptualizing what I need to do next...

    The problem seems to stem from the fact that I can't just make a temporary array outright, its size much match the size of the input array (a in this case) in order for the values to be copied whether by loop or memcpy... This appears to be where malloc comes in to help out.

    Would you mind elaborating a bit please? I'm not sure exactly how I implement malloc.

  3. #18
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, there are ways to do this, but malloc() is the best in terms of having easiness, neatness. For your compiler which doesn't use the C99 standard I suppose.

    If you cannot use malloc() then the next best thing is to declare a maximum size of the
    array. Then you can just use temp[maximum_size].

    If you are uncertain of the size or it is too big then you should pass a duplicate of the array inside the function. Like void reverse(int a[], int temp[], int size). You would have the duplicate array in main of course.

    MORE importantly you have to think this. How are the values of a passed to the program. If you have a[] = {2,1,3,4; then you now that you have 4 elements in a. So you you declare int temp[4]. The point of passing size in the reverse function is pointless! It has no use, you know the size. It is 4.
    Now. If the values where passed from a user then you would have to declare a with the size of the input entered in the first place before worrying about anything else! This can only be done with malloc() or by declaring again the array with a maximum size.
    In any case, whatever you do with a you can do with temp. If you declare a as a 4-element array you already did, you should do that for temp

    EDIT:
    you would do:
    Code:
    int* temp;
    temp = malloc(size*sizeof(int));
    Thus allocating space. Not that since temp is a pointer you can use []. Then you use it as you did. Before the end of the function you would have to do:
    Code:
    free(temp);
    To free the memory (not that it won't work if you don't)
    Google malloc()/free and search for more details
    Last edited by C_ntua; 10-14-2008 at 02:49 PM.

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by C_ntua View Post
    Well, there are ways to do this, but malloc() is the best in terms of having easiness, neatness. For your compiler which doesn't use the C99 standard I suppose.

    If you cannot use malloc() then the next best thing is to declare a maximum size of the
    array. Then you can just use temp[maximum_size].

    If you are uncertain of the size or it is too big then you should pass a duplicate of the array inside the function. Like void reverse(int a[], int temp[], int size). You would have the duplicate array in main of course.

    MORE importantly you have to think this. How are the values of a passed to the program. If you have a[] = {2,1,3,4; then you now that you have 4 elements in a. So you you declare int temp[4]. The point of passing size in the reverse function is pointless! It has no use, you know the size. It is 4.
    Now. If the values where passed from a user then you would have to declare a with the size of the input entered in the first place before worrying about anything else! This can only be done with malloc() or by declaring again the array with a maximum size.
    In any case, whatever you do with a you can do with temp. If you declare a as a 4-element array you already did, you should do that for temp
    Yep, I realize this. The instructions for this work by the instructor were to leave the headers and names exactly as he gave them. All roads appear to point to using malloc in one form or another.

  5. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by C_ntua View Post
    Well, there are ways to do this, but malloc() is the best in terms of having easiness, neatness. For your compiler which doesn't use the C99 standard I suppose.

    If you cannot use malloc() then the next best thing is to declare a maximum size of the
    array. Then you can just use temp[maximum_size].

    If you are uncertain of the size or it is too big then you should pass a duplicate of the array inside the function. Like void reverse(int a[], int temp[], int size). You would have the duplicate array in main of course.

    MORE importantly you have to think this. How are the values of a passed to the program. If you have a[] = {2,1,3,4; then you now that you have 4 elements in a. So you you declare int temp[4]. The point of passing size in the reverse function is pointless! It has no use, you know the size. It is 4.
    Now. If the values where passed from a user then you would have to declare a with the size of the input entered in the first place before worrying about anything else! This can only be done with malloc() or by declaring again the array with a maximum size.
    In any case, whatever you do with a you can do with temp. If you declare a as a 4-element array you already did, you should do that for temp

    EDIT:
    you would do:
    Code:
    int* temp;
    temp = malloc(size*sizeof(int));
    Thus allocating space. Not that since temp is a pointer you can use []. Then you use it as you did. Before the end of the function you would have to do:
    Code:
    free(temp);
    To free the memory (not that it won't work if you don't)
    Google malloc()/free and search for more details
    I'm getting an error from this:

    error C2440: '=' : cannot convert from 'void *' to 'int *'

    I know malloc is what is causing it, I'm just not sure how to remedy the problem. Casting perhaps? I vaguely remember reading somewhere that there's a way to do this..

    [edit] - Casted it and the program works perfectly now. Is casting the proper way though?

    [edit2] - Another question-- is if temp is a pointer (it points to the memory that malloc made), how come I didn't need to dereference it to get the index in my loop? I think my instructor said that brackets automatically dereference or something... but the proper way to write it would be:

    a[j] = i[*temp]

    instead of

    a[j] = temp[i]


    Any input on this?
    Last edited by Sparrowhawk; 10-14-2008 at 03:12 PM.

  6. #21
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Look. For a pointer this is equal:
    temp[10] and *(temp+10). That is what the index means. The value of the memory (dereference) 10 spaces after temp.

    Well, Elysia would disagree about the casting, as would this site FAQ. So read the FAQ for more information. Are you compiling this with a C compiler or a C++ compiler? In Visual Studio C++ I believe if the file is .c it is a C compiler. If it is a .cpp it is a C++ compiler. A C++ compiler would give an error. A C compiler should not (maybe a warning).

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by C_ntua View Post
    Look. For a pointer this is equal:
    temp[10] and *(temp+10). That is what the index means. The value of the memory (dereference) 10 spaces after temp.

    Well, Elysia would disagree about the casting, as would this site FAQ. So read the FAQ for more information. Are you compiling this with a C compiler or a C++ compiler? In Visual Studio C++ I believe if the file is .c it is a C compiler. If it is a .cpp it is a C++ compiler. A C++ compiler would give an error. A C compiler should not (maybe a warning).
    It's a .cpp file, the compiler didn't give an error or warning however. I'll read the FAQ now, thanks.

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    I'm getting an error from this:

    error C2440: '=' : cannot convert from 'void *' to 'int *'
    the compiler didn't give an error or warning however.
    You'll have to pick one. (The point is that the warning is only C++; it's valid (and preferred to the alternative) in C.)

  9. #24
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by tabstop View Post
    You'll have to pick one. (The point is that the warning is only C++; it's valid (and preferred to the alternative) in C.)
    I think you misread . It gave an error before casting, but not after casting the void* as an int*.

    The class I'm taking deals strictly with that which is in the realm of C... We aren't allowed to use any C++.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    Quote Originally Posted by Sparrowhawk View Post
    I think you misread . It gave an error before casting, but not after casting the void* as an int*.
    That's exactly what we're saying. In C you don't cast. But because you named your file whatever.cpp, your compiler thinks it's C++, so it's using C++ rules, which state that you must cast.

  11. #26
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by tabstop View Post
    That's exactly what we're saying. In C you don't cast. But because you named your file whatever.cpp, your compiler thinks it's C++, so it's using C++ rules, which state that you must cast.
    Ah ok, I understand. I believe you mean that in C you don't have to cast, but you can... Just in this case it isn't required in C... correct?

    I'm aware that casting as an int will end up screwing me later if I pass the array something besides an int... I know that in C the purpose of doing malloc (sizeof(whateveryourarraynameishere)) is to avoid that, but it wouldn't accept that so there you are .
    Last edited by Sparrowhawk; 10-14-2008 at 05:10 PM.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,653
    Yes, the correct way is to cast.
    I would always like to recommend casting, even if it's C, since there are tools that can detect such mistakes.
    C++ is very type strict, which means that unlike C, if your types don't match (eg assign void* to int*), the compiler will produce an error. You can get around this by telling the compiler that you really meant it, and you do that with a cast.
    Don't worry about using a C++ compiler to compile your C code - it won't harm you. You just need to be aware that you may need casts where you wouldn't need in C (but casts are a good thing, if you ask me).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Zipping files
    By CompiledMonkey in forum C Programming
    Replies: 19
    Last Post: 03-06-2003, 12:23 PM